/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AssignComputerToPersonDialog.java
 *
 * Created on 13 Ιαν 2011, 1:27:36 μμ
 */

package repomanagement.GUI;

import repomanagement.entities.*;
import repomanagement.controller.DBManager;
import java.util.ArrayList;
import javax.swing.UIManager;
import javax.swing.JOptionPane;

/**
 *
 * @author ndrosos
 */
public class AssignComputerToPersonDialog extends javax.swing.JDialog {
    private DBManager dbM;
    private Person selectedPerson;

    /** Creates new form AssignComputerToPersonDialog */
    public AssignComputerToPersonDialog(java.awt.Frame parent, boolean modal, DBManager dbM) {
        super(parent, modal);
        // Θέτει το LookANdFeel του παραθύρου
        try {
             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.dbM = dbM;

        initComponents();

        // Θέτει ως μοντέλο της λίστας jList1 που εμφανίζει το προσωπικό, το PersonListModel
        // που ορίζεται ως inner κλάση
        jList1.setModel(new PersonListModel());
        // Επιτρέπει απλή επιλογή αντικειμένων της λίστας
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        // Ακούει για αλλαγές στην επιλογή αντικειμένων της λίστας και κάθε φορά καλεί την
        // jList1ValueChanged(evt) για να ανανεώσει τα περιεχόμενα της λίστας με τους χρεωμένους
        // υπολογιστές
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });

        // Θέτει ως μοντέλο της λίστας jList2 που εμφανίζει τους χρεωμένους υπολογιστές,
        // το AssignedComputerListModel που ορίζεται ως inner κλάση
        jList2.setModel(new AssignedComputerListModel());
         // Επιτρέπει απλή επιλογή αντικειμένων της λίστας
        jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        // Θέτει ως μοντέλο της λίστας jList3 που εμφανίζει τους διαθέσιμους υπολογιστές,
        // το AvailableComputerListModel που ορίζεται ως inner κλάση
        jList3.setModel(new AvailableComputerListModel());
         // Επιτρέπει απλή επιλογή αντικειμένων της λίστας
        jList3.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

    }

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {
        Person p1 = (Person) jList1.getSelectedValue();
        selectedPerson = p1;
        ((AssignedComputerListModel) jList2.getModel()).setSelectedPerson(p1);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(repomanagement.RepoManagementApp.class).getContext().getResourceMap(AssignComputerToPersonDialog.class);
        jLabel1.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setOpaque(true);

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList3.setAlignmentX(0.0F);
        jList3.setAlignmentY(0.0F);
        jList3.setName("jList3"); // NOI18N
        jScrollPane2.setViewportView(jList3);

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jList2.setAlignmentX(0.0F);
        jList2.setAlignmentY(0.0F);
        jList2.setName("jList2"); // NOI18N
        jScrollPane3.setViewportView(jList2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 671, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jLabel2)
                        .addGap(230, 230, 230)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 393, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane3)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jButton1)
                        .addGap(53, 53, 53)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Κλείνει το παράθυρο
        dispose();
}//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Χρεώνει έναν υπολογιστή στο selectedPerson και καταχωρεί την χρέωση στη βάση
        if (selectedPerson != null){
            Computer c = (Computer) jList3.getSelectedValue();
            if (c!=null){
                ((AssignedComputerListModel) jList2.getModel()).addAssignedComputer(c);
                ((AvailableComputerListModel) jList3.getModel()).removeAvailableComputer(c);
                dbM.assignComputerToPerson(c, selectedPerson);
                jList3.clearSelection();
            } else
                JOptionPane.showMessageDialog(this, "ΣΦΑΛΜΑ: Πρέπει να επιλέξετε ένα διαθέσιμο υπολογιστή!");
        } else
            JOptionPane.showMessageDialog(this, "ΣΦΑΛΜΑ: Πρέπει να επιλέξετε ένα άτομο!");
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Ξεχρεώνει έναν υπολογιστή και ενημερώνει τη βάση
        if (selectedPerson != null){
            Computer c = (Computer) jList2.getSelectedValue();
            if (c!=null){
                ((AvailableComputerListModel) jList3.getModel()).addAvailableComputer(c);
                ((AssignedComputerListModel) jList2.getModel()).removeAssignedComputer(c);
                dbM.releaseComputerFromPerson(c);
                jList2.clearSelection();
            } else
                JOptionPane.showMessageDialog(this, "ΣΦΑΛΜΑ: Πρέπει να επιλέξετε έναν χρεωμένο υπολογιστή!");
        } else
            JOptionPane.showMessageDialog(this, "ΣΦΑΛΜΑ: Πρέπει να επιλέξετε ένα άτομο!");
}//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AssignComputerToPersonDialog dialog = new AssignComputerToPersonDialog(new javax.swing.JFrame(), true, new DBManager());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables

     class PersonListModel extends javax.swing.AbstractListModel {

        ArrayList<Person> persons = dbM.retrievePersons();

        public int getSize() {
            return persons.size();
        }

        public Object getElementAt(int i) {
            return persons.get(i);
        }

        public void addPerson(Person x) {
            persons.add(x);
            fireContentsChanged(this, persons.size() - 1, persons.size() - 1);
        }
    }

    class AssignedComputerListModel extends javax.swing.AbstractListModel {

        ArrayList<Computer> assignedComputers = new ArrayList<Computer>();

        public void setSelectedPerson(Person selectedPerson) {
            if (selectedPerson == null)
                return;
            assignedComputers = dbM.retrieveAssignedComputers(selectedPerson);
            fireContentsChanged(this, assignedComputers.size() - 1, assignedComputers.size() - 1);
        }

        public int getSize() {
            return assignedComputers.size();
        }

        public Object getElementAt(int i) {
            return assignedComputers.get(i);
        }

        public void addAssignedComputer(Computer x) {
            assignedComputers.add(x);
            fireContentsChanged(this, assignedComputers.size() - 1, assignedComputers.size() - 1);
        }

        public void removeAssignedComputer(Computer x) {
            assignedComputers.remove(x);
            fireContentsChanged(this, assignedComputers.size() - 1, assignedComputers.size() - 1);
        }
    }

    class AvailableComputerListModel extends javax.swing.AbstractListModel {

        ArrayList<Computer> availableComputers = dbM.retrieveAvailableComputers();

        public int getSize() {
            return availableComputers.size();
        }

        public Object getElementAt(int i) {
            return availableComputers.get(i);
        }

        public void addAvailableComputer(Computer x) {
            availableComputers.add(x);
            fireContentsChanged(this, availableComputers.size() - 1, availableComputers.size() - 1);
        }

        public void removeAvailableComputer(Computer x) {
            availableComputers.remove(x);
            fireContentsChanged(this, availableComputers.size() - 1, availableComputers.size() - 1);
        }
    }
}
